package com.webserver.servlet;




import com.webserver.http.HttpRequest;
import com.webserver.http.HttpRseponse;

import java.io.File;

/**
 * 用于完成处理请求环节的操作
 */
public class DispatcherServlet {

    public void service(HttpRequest request,HttpRseponse rseponse){
        String path = request.getUri();

        File file = new File("./webapps"+path);
        //如果该资源存在,并且是一个文件则响应文件
        if (file.isFile() && file.exists()) {
            rseponse.setEntity(file);

        }else {//否则响应404
            rseponse.setStatusCode(404);
            rseponse.setStatusReason("NotFound");
            file = new File("./webapps/root/404.html");
            rseponse.setEntity(file);

            System.out.println("抽象路径为:./webapps/root/404.html");
        }
        //Server响应头用来告知浏览器,服务端是谁
        rseponse.putHeader("Server","WebServer");
    }
}
